<?php

require_once("BaseDAO.php");
require_once("../Entities/Word.php");

class WordDAO extends BaseDAO
{
	function CreateWord($item)
	{
		$sql = "select * from Words where Key='" . $item->Key . "' and TypeWord='" . $item->TypeWord . "' and CreatedBy='" . $item->CreatedBy . "';";
		$result = mysql_query($sql);
		if(mysql_num_rows($result) == 0)
		{
			$sql = "insert into Words(Key , Translate, TypeWord, CreatedBy, CreationOn) values ('" . 
				$item->Key . "','" . $item->Translate . "','" . $item->TypeWord . "','" . $item->CreatedBy . "',NOW());";
			mysql_query($sql);
			$item->Id = mysql_insert_id();
			return $item;	
		}
		else
		{
			return null;
		}
	}
	
	function UpdateWord($item)
	{
		$sql = "update Words set Key='" . $item->Key . "',TypeWord='" . 
			$item->TypeWord . "',Translate='" . $item->Translate . "' where Id='" . $item->Id . "';";
		mysql_query($sql);
	}
	
	function DeleteWord($id)
	{
		$sql = "delete from Words where Id='" . $id . "';";
		mysql_query($sql);
	}
	
	function GetWordById($id)
	{
		$sql = "select * from Words where Id='" . $id . "';";
		$result = mysql_query($sql);
		if(mysql_num_rows($result) == 1)
		{
			$row = mysql_fetch_assoc($result);
			$item = new Word();
			$item->Id = $row["Id"];
			$item->Name = $row["Key"];
			$item->TypeWord = $row["TypeWord"];
			$item->Translate = $row["Translate"];
			$item->CreatedBy = $row["CreatedBy"];
			$item->CreationOn = $row["CreationOn"];
			return $item;
		}
		return null;
	}
	
	function GetWordByKey($name, $createdBy, $type)
	{
		$sql = "select * from Words where Key='" . $item->Key . "' and TypeWord='" . $item->TypeWord . "' and CreatedBy='" . $item->CreatedBy . "';";
		$result = mysql_query($sql);
		if(mysql_num_rows($result) == 1)
		{
			$row = mysql_fetch_assoc($result);
			$item = new Word();
			$item->Id = $row["Id"];
			$item->Name = $row["Key"];
			$item->TypeWord = $row["TypeWord"];
			$item->Translate = $row["Translate"];
			$item->CreatedBy = $row["CreatedBy"];
			$item->CreationOn = $row["CreationOn"];
			return $item;
		}
		return null;
	}
	
	function GetWordsLikeName($name, $createdBy, $type)
	{
		$sql = "select * from Words where Key like %" . $name . "% and CreatedBy='" . $createdBy . "' and TypeWord='" . $type . "';";
		$result = mysql_query($sql);
		$kq = array();
		while($row = mysql_fetch_assoc($result))
		{
			$item = new Word();
			$item->Id = $row["Id"];
			$item->Name = $row["Key"];
			$item->TypeWord = $row["TypeWord"];
			$item->Translate = $row["Translate"];
			$item->CreatedBy = $row["CreatedBy"];
			$item->CreationOn = $row["CreationOn"];
			$kq[] = $item;
		}
		return $kq;
	}
}

?>